<template>
	<view>
		<view class="uni-header">
			<uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
		</view>

		<view class="uni-container">
			<div id="sys_index">
				<el-tabs v-model="activeName">
					<el-tab-pane label="定时任务设置" name="first">
						<el-card class="card">
							<div slot="header" class="clearfix">
								<span>定时生成存档备份任务</span>
							</div>
							<div style="margin: 5px" v-for="item in scheduleVO.backupTimeList">
								执行时间：{{item.time}},

								执行状态：
								<strong style="color: green" v-if="item.count > 0">
									已经执行</strong>
								<strong style="color: orange" v-if="item.count === 0">
									未执行</strong>
							</div>

							<el-row style="margin: 5px">
								<el-col :span="5">
									<el-button :size="size" type="primary" @click="addBackupTime()">添加 执行时间</el-button>
								</el-col>
							</el-row>

							<block v-for="(item,key) in backupTimeList">
								<div style="margin: 5px">
									<el-time-picker style="width:120px" :size="size" placeholder="请选择 执行时间"
										v-model="item.time" clearable></el-time-picker>
									<el-button :size="size" type="warning" style="margin-left: 5px"
										@click="delBackupTime(key)">删除</el-button>
								</div>
							</block>
						</el-card>
						<el-card class="card">
							<div slot="header" class="clearfix">
								<span>定时生成世界任务</span>
								<span style="color: red;margin-left: 40px">智能重置【两小时服务器无人重置世界】：</span>
								<el-switch v-model="smartCreate" active-text="开" inactive-text="关"></el-switch>
							</div>
							<div style="margin: 5px" v-for="item in scheduleVO.createTimeList">
								执行时间：{{item.time}},

								执行状态：
								<strong style="color: green" v-if="item.count > 0">
									已经执行</strong><strong style="color: orange" v-if="item.count === 0">
									未执行</strong>
							</div>

							<el-row style="margin: 5px">
								<el-col :span="5">
									<el-button :size="size" type="primary" @click="addCreateTime()">添加 执行时间</el-button>
								</el-col>
							</el-row>

							<template v-for="(item,key) in createTimeList">
								<div style="margin: 5px">
									<el-time-picker style="width:120px" :size="size" placeholder="请选择 执行时间"
										v-model="item.time" clearable></el-time-picker>
									<el-button :size="size" type="warning" style="margin-left: 5px"
										@click="delCreateTime(key)">删除</el-button>
								</div>
							</template>
						</el-card>

						<el-card class="card">
							<div slot="header" class="clearfix">
								<span>
									定时更新游戏任务
								</span>
								<span style="color: red;margin-left: 40px">更新时不启动：</span>
								<el-checkbox v-model="notStartMaster">地面</el-checkbox>
								<el-checkbox v-model="notStartCaves">洞穴</el-checkbox>

								<span style="color: red;margin-left: 40px">智能更新：</span>
								<el-switch v-model="smartUpdate" active-text="开" inactive-text="关"></el-switch>
							</div>
							<div v-for="item in scheduleVO.updateTimeList">执行时间
								：{{item.time}}, 执行状态：
								<strong style="color: green" v-if="item.count > 0">
									已经执行</strong><strong style="color: orange" v-if="item.count === 0">
									未执行</strong>
							</div>
							<el-row style="margin: 5px">
								<el-col :span="5">
									<el-button :size="size" type="primary" @click="addUpdateTime()">添加 执行时间</el-button>
								</el-col>
							</el-row>

							<template v-for="(item,key) in updateTimeList">
								<div style="margin: 5px">
									<el-time-picker :size="size" style="width:120px" placeholder="请选择 执行时间"
										v-model="item.time" clearable></el-time-picker>
									<el-button :size="size" type="warning" style="margin-left: 5px"
										@click="delUpdateTime(key)">删除</el-button>
								</div>
							</template>
						</el-card>

						<el-card style="margin: 10px; position: sticky; bottom: 0;  z-index: 10;">
							<el-button :size="size" type="primary" @click="saveSchedule()">保存</el-button>
						</el-card>
					</el-tab-pane>
					<el-tab-pane class="panetype" label="世界运行日志" name="second" style="height: auto;">
						<el-card class="card" style="position: relative;height: 100vh;">
							<el-row style="margin: 5px;" >
								<el-col :span="3" >
									<uni-data-select v-model="Type" :placeholder="TypeName" :localdata="worldList"
										@change="newsPage"></uni-data-select>
								</el-col>
								<el-col :span="3">
									<el-input :size="size" placeholder="请输入需要拉取的日志行数" v-model="num1" type="number"
										clearable></el-input>

								</el-col>
								<el-col :span="6">
									<el-button :size="size" type="primary" @click="getDstLog(1,num1)">拉取</el-button>
								</el-col>
							</el-row>
							<el-row>
								<el-card class="card">
									<ul>
										<li v-for="log in worldLog">{{ log }}</li>
									</ul>
								</el-card>
								
							</el-row>
						</el-card>
					</el-tab-pane>
					<el-tab-pane label="实验室" name="fifth">
						<el-card class="card">
							<div slot="header" class="clearfix">
								<span>智能更新</span>
							</div>
							<ul>
								<li>Klei 饥荒最新版本号：
									<strong style="color: green">{{versionMap.steamVersion}}</strong>
								</li>
								<li>当前服务器的版本号：
									<strong style="color: green">{{versionMap.localVersion}}</strong>
								</li>
							</ul>
						</el-card>
					</el-tab-pane>
				</el-tabs>
			</div>
		</view>
	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				Type: "",
				activeName: 'first',
				num1: 20,
				worldLog: [],
				worldList: [],
				TypeName: "请选择世界~",
				scheduleVO: {

				},
				updateTimeList: [],
				backupTimeList: [],
				createTimeList: [],
				notStartMaster: false,
				notStartCaves: false,
				smartUpdate: false,
				smartCreate: false,
				versionMap: {},
				model: {
					backupTimeList: [],
					updateTimeList: [],
					createTimeList: [],
				},
				labelPosition: 'left',
				size: 'medium',
			}
		},
		created() {
			this.getScheduleList();
			this.getVersion();
			this.getLabelPosition();
			this.getWorldList()
		},
		mounted() {
			window.onresize = () => {
				this.getLabelPosition()
			}
		},
		methods: {
			getLabelPosition() {
				let windowWidth = window.innerWidth
				console.log('getLabelPosition', windowWidth)
				if (windowWidth < 768) {
					this.labelPosition = 'top'
					this.size = 'mini'
				} else {
					this.labelPosition = 'left'
					this.size = 'medium'
				}

			},
			getScheduleList() {
				this.$http.get("/system/getScheduleList").then((data) => {
					this.scheduleVO = data.data;
					//初始化
					this.updateTimeList = [];
					this.backupTimeList = [];
					this.createTimeList = [];

					if (this.scheduleVO.createTimeList) {
						this.scheduleVO.createTimeList.forEach(e => {
							let obj = {};
							obj.time = "2020-10-23 " + e.time;
							obj.count = e.count;
							this.createTimeList.push(obj);
						})
					}
					if (this.scheduleVO.updateTimeList) {
						this.scheduleVO.updateTimeList.forEach(e => {
							let obj = {};
							obj.time = "2020-10-23 " + e.time;
							obj.count = e.count;
							this.updateTimeList.push(obj);
						})
					}
					if (this.scheduleVO.backupTimeList) {
						this.scheduleVO.backupTimeList.forEach(e => {
							let obj = {};
							obj.time = "2020-10-23 " + e.time;
							obj.count = e.count;
							this.backupTimeList.push(obj);
						})
					}
					this.notStartMaster = data.data.notStartMaster ? data.data.notStartMaster : false;
					this.notStartCaves = data.data.notStartCaves ? data.data.notStartCaves : false;
					this.smartUpdate = data.data.smartUpdate ? data.data.smartUpdate : false;
					this.smartCreate = data.data.smartCreate ? data.data.smartCreate : false;
				})
			},
			getVersion() {
				this.$http.get("/system/getVersion").then((data) => {
					this.versionMap = data.data;
				});
			},
			addUpdateTime() {
				this.updateTimeList.push({
					count: 0,
					time: '2020-10-23 06:00:00'
				});
			},
			delUpdateTime(key) {
				this.updateTimeList.splice(key, 1);
			},
			addBackupTime() {
				this.backupTimeList.push({
					count: 0,
					time: '2020-10-23 06:00:00'
				});
			},
			delBackupTime(key) {
				this.backupTimeList.splice(key, 1);
			},
			addCreateTime() {
				this.createTimeList.push({
					count: 0,
					time: '2020-10-23 06:00:00'
				});
			},
			delCreateTime(key) {
				this.createTimeList.splice(key, 1);
			},
			//格式化时间
			formatTime(date, fmt) {
				if (date == null || date == undefined || date == '') {
					return null;
				}
				var date = new Date(date);
				if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
				}
				var o = {
					'M+': date.getMonth() + 1,
					'd+': date.getDate(),
					'h+': date.getHours(),
					'm+': date.getMinutes(),
					's+': date.getSeconds()
				};
				for (var k in o) {
					if (new RegExp('(' + k + ')').test(fmt)) {
						var str = o[k] + '';
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
					}
				}
				return fmt;
			},
			saveSchedule() {
				let params = {};
				params.backupTimeList = [];
				params.updateTimeList = [];
				params.createTimeList = [];
				if (this.createTimeList.length > 0) {
					this.createTimeList.forEach(e => {
						let formatTime = this.formatTime(e.time, "yyyy-MM-dd hh:mm:ss");
						let obj = {
							time: formatTime,
							count: e.count
						};
						params.createTimeList.push(obj);
					})
				}
				if (this.backupTimeList.length > 0) {
					this.backupTimeList.forEach(e => {
						let formatTime = this.formatTime(e.time, "yyyy-MM-dd hh:mm:ss");
						let obj = {
							time: formatTime,
							count: e.count
						};
						params.backupTimeList.push(obj);
					})
				}
				if (this.updateTimeList.length > 0) {
					this.updateTimeList.forEach(e => {
						let formatTime = this.formatTime(e.time, "yyyy-MM-dd hh:mm:ss");
						let obj = {
							time: formatTime,
							count: e.count
						};
						params.updateTimeList.push(obj);
					})
				}

				params.notStartMaster = this.notStartMaster ? this.notStartMaster : false;
				params.notStartCaves = this.notStartCaves ? this.notStartCaves : false;
				params.smartUpdate = this.smartUpdate ? this.smartUpdate : false;
				params.smartCreate = this.smartCreate ? this.smartCreate : false;
				console.log(params);
				this.$http.post("/system/saveSchedule", params).then((data) => {
					if (data.code != 0) {
						this.$message({
							message: data.message,
							type: 'warning'
						});
					} else {
						this.$message({
							message: '保存成功',
							type: 'success'
						});
						this.getScheduleList()
					}
				})
			},
			getDstLog(type, rowNum) {
				this.$http.get("/system/getDstLog?type=" + type + "&rowNum=" + rowNum).then((data) => {
					this.worldLog = data.data;
				})
			},
			getWorldList() {
				this.$http.get("/system/getWorldList").then((data) => {
					if (data.data) {
						let arr = []
						for (var i = 0; i < data.data.length; i++) {
							
							arr.push({
								text: data.data[i],
								value: i + 1
							})

						}

						this.worldList = arr;
					}
				})
			},
		}
	}
</script>

<style scoped>
	>>> .el-card {
		overflow: unset;
	}
</style>